package educrypt.gui.components;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.ConsoleSmallView;
import educrypt.gui.view.EducryptFileChooserView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class ConsoleSmallPanel extends EducryptPanel implements ConsoleSmallView {

	private static final long serialVersionUID = 3912832154602292036L;

	private JButton btnSaveLog, btnClearLog, btnPreview;
	private JTextPane txtLog;
	private EducryptFileChooser saveFileDialog;

	public ConsoleSmallPanel() {
		this.setSize(740, 170);
		this.setPreferredSize(this.getSize());
		setLayout(null);

		JLabel lblConsole = new JLabel(Messages.getString("guiConsole"));
		lblConsole.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblConsole.setBounds(30, 11, 82, 19);
		add(lblConsole);

		JScrollPane scrollPane = new JScrollPane((Component) null);
		scrollPane.setBounds(30, 41, 654, 114);
		add(scrollPane);

		txtLog = new JTextPane();
		txtLog.setEditable(false);
		scrollPane.setViewportView(txtLog);

		ImageIcon saveIcon = new ImageIcon(
				EduCryptResources.getImageURL("save24.png"));

		btnSaveLog = new JButton(saveIcon);
		btnSaveLog.setBounds(694, 127, 28, 28);
		add(btnSaveLog);

		ImageIcon clearLogIcon = new ImageIcon(
				EduCryptResources.getImageURL("remove24color.png"));

		btnClearLog = new JButton(clearLogIcon);
		btnClearLog.setBounds(694, 72, 28, 28);
		add(btnClearLog);

		ImageIcon previewIcon = new ImageIcon(
				EduCryptResources.getImageURL("preview24.png"));

		btnPreview = new JButton(previewIcon);
		btnPreview.setBounds(694, 41, 28, 28);
		add(btnPreview);

		saveFileDialog = new EducryptFileChooser(btnSaveLog);
		saveFileDialog.setTxtMode();
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnClearLog.setActionCommand(CLEAR_LOG);
		btnClearLog.addActionListener(ctr);

		btnPreview.setActionCommand(PREVIEW_LOG);
		btnPreview.addActionListener(ctr);

		btnSaveLog.setActionCommand(SAVE_LOG);
		btnSaveLog.addActionListener(ctr);

		saveFileDialog.addListener(ctr);
	}

	@Override
	public void clearLog() {
		try {
			this.txtLog.getStyledDocument().remove(0,
					txtLog.getStyledDocument().getLength());
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void addLogMessage(int type, String text) {
		SimpleAttributeSet attrs = GuiUtil.formatConsoleMessage(type);
		try {
			txtLog.getStyledDocument().insertString(
					txtLog.getStyledDocument().getLength(), text + "\n", attrs);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public EducryptFileChooserView getSaveFileView() {
		return saveFileDialog;
	}

	@Override
	public String getLog() {
		return txtLog.getText();
	}
}
